package com.atom.app.mvvm.ui.activities

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import androidx.viewpager2.widget.ViewPager2
import com.atom.app.mvvm.AppModel
import com.atom.app.mvvm.BR
import com.atom.app.mvvm.R
import com.atom.app.mvvm.VMFactory
import com.atom.app.mvvm.Contacts
import com.atom.app.mvvm.databinding.ActivityWelcomeBinding
import com.atom.app.mvvm.db.Image
import com.atom.app.mvvm.utils.AbstractAdapter
import com.atom.app.mvvm.utils.AbstractViewHolder
import com.atom.module.logger.Logger
import com.atom.module.mvvm.base.view.activity.AbstractBaseMVVMActivity
import com.atom.module.mvvm.base.viewmodel.AbstractBaseViewModel
import com.atom.module.mvvm.util.Auto
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.launch

class WelcomeActivity : AbstractBaseMVVMActivity<ActivityWelcomeBinding, WelcomeViewModel>() {

    private val layouts: IntArray = intArrayOf(
        R.layout.tutorial_slide1,
        R.layout.tutorial_slide2,
        R.layout.tutorial_slide3,
        R.layout.tutorial_slide4
    )

    override fun initArgs(intent: Intent ,savedInstanceState: Bundle?): Boolean {
        Auto.registerTask(this::class) { app ->
            binding.viewPager2.currentItem = layouts.size - 1
            if (binding.btnSkip.isVisible) {
                binding.btnSkip.callOnClick()
                true
            } else {
                false
            }
        }
        return super.initArgs(intent ,savedInstanceState)
    }

    override fun initContentView(savedInstanceState: Bundle?): Int {
        return R.layout.activity_welcome
    }

    override fun initVariableId(): Int {
        return BR._all
    }

    override fun initViewModel(): WelcomeViewModel {
        return ViewModelProvider(this, VMFactory).get(WelcomeViewModel::class.java)
    }

    override fun initData(view: View) {
        super.initData(view)
        val adapter = object : AbstractAdapter<Int>(layouts.asList()) {
            override fun getItemViewType(position: Int, data: Int): Int {
                return layouts[position % layouts.size]
            }

            override fun onCreateViewHolder(
                contentView: View,
                viewType: Int
            ): AbstractViewHolder<Int> {
                return ViewHolder(contentView)
            }
        }
        binding.viewPager2.adapter = adapter
        binding.viewPager2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                initBottomDots(position)
                // changing the next button text 'NEXT' / 'GOT IT'
                if (position == layouts.size - 1) {
                    // last page. make button text to GOT IT
                    binding.btnNext.text = getString(R.string.okay)
                    binding.btnSkip.visibility = View.GONE
                } else {
                    // still pages are left
                    binding.btnNext.text = getString(R.string.next)
                    binding.btnSkip.visibility = View.VISIBLE
                }
            }
        })

        binding.btnSkip.setOnClickListener {
            if (MMKV.defaultMMKV().decodeBool(Contacts.tag_is_first)) {
                val intent = Intent(
                    this@WelcomeActivity,
                    MainActivity::class.java
                )
                intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
                startActivity(intent)
            }
            finish()
        }
        binding.btnNext.setOnClickListener {
            // checking for last page
            // if last page home screen will be launched
            val current: Int = binding.viewPager2.currentItem + 1
            if (current < layouts.size) {
                // move to next screen
                binding.viewPager2.currentItem = current
            } else {
                binding.btnSkip.callOnClick()
            }
        }

        binding.root.post {
            initBottomDots()
        }
    }

    fun initBottomDots(position: Int = 0) {
        val activeColor = ContextCompat.getDrawable(this, R.drawable.shape_bg_dot_light)
        val inactiveColor = ContextCompat.getDrawable(this, R.drawable.shape_bg_dot_dark)
        binding.layoutDots.removeAllViews()
        val params = LinearLayout.LayoutParams(30, 30)
        params.setMargins(30, 0, 30, 0)
        for (i in layouts.indices) {
            val image = ImageView(this)
            if (position == i) {
                image.background = activeColor
            } else {
                image.background = inactiveColor
            }
            binding.layoutDots.addView(
                image, params
            )
        }
    }

    class ViewHolder(view: View) : AbstractViewHolder<Int>(itemView = view) {
        override fun onBind(data: Int) {

        }
    }

    override fun onResume() {
        super.onResume()
        Auto.startTask(this)
    }
}

class WelcomeViewModel : AbstractBaseViewModel<AppModel>(AppModel) {

    fun insertImage() {
        val launch = viewModelScope.launch {
            val image = Image()
            Logger.e(image, AppModel.imageDao.insert(image))
        }
        Logger.e(launch.isActive, launch.isCancelled, launch.isCancelled)
    }

    fun selectAll() = viewModelScope.launch {
        val selectAll = AppModel.imageDao.selectAll()
        Logger.e(selectAll)
    }

}
